package com.egao.api.controller.nont;

import com.alibaba.fastjson.JSONObject;
import com.egao.api.base.BaseApiController;
import com.egao.common.core.constants.WechatConstant;
import com.egao.common.core.utils.HttpClientUtil;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.common.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wf.jwtp.provider.Token;
import org.wf.jwtp.provider.TokenStore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : Doman
 * @version ：1.0
 * @date : 2020-04-12 17:33
 */
//@RestController
//@RequestMapping("/api/nt/wechart")
public class WechatApi extends BaseApiController {

    @Autowired
    private UserService userService;
    @Autowired
    private TokenStore tokenStore;


    @PostMapping("/login")
    @ResponseBody
    public JsonResult user_login(String code, HttpServletRequest request){
        String code2 = request.getParameter("code");
        // 配置请求参数
        Map<String, String> param = new HashMap<>();
        param.put("appid", WechatConstant.WX_LOGIN_APPID);
        param.put("secret", WechatConstant.WX_LOGIN_SECRET);
        param.put("js_code", code);
        param.put("grant_type", WechatConstant.WX_LOGIN_GRANT_TYPE);
        // 发送请求
        String wxResult = HttpClientUtil.doGet(WechatConstant.WX_LOGIN_URL, param);
        JSONObject jsonObject = JSONObject.parseObject(wxResult);

        // 获取参数返回的
        String session_key = jsonObject.get("session_key").toString();
        String open_id = jsonObject.get("openid").toString();
        // 根据返回的user实体类，判断用户是否是新用户，不是的话，更新最新登录时间，是的话，将用户信息存到数据库
        User user = null;
        try {
//            user = userService.getByOpenId(new User(open_id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*if(user != null){
            user.setUserNewLogin(new Date());
            userService.updateById(user);
        }else{
            User insert_user = new User();
            insert_user.setUserHead(userHead);
            insert_user.setUserName(userName);
            insert_user.setUserGender(userGender);
            insert_user.setUserNewLogin(new Date());
            insert_user.setUserCity(userCity);
            insert_user.setUserProvince(userProvince);
            insert_user.setUserOpenid(open_id);
            System.out.println("insert_user:"+insert_user.toString());
            // 添加到数据库
            Boolean flag = userService.insert(insert_user);
            if(!flag){
                return JsonResult.error();
            }
        }*/
        user = new User();
        user.setUserId(1);
        // 封装返回小程序
        Map<String, String> result = new HashMap<>();
        result.put("session_key", session_key);
        result.put("open_id", open_id);
//        return JsonResult.ok().put("session_key", session_key).put("open_id", open_id);
        // 签发token
        Token token = tokenStore.createNewToken(String.valueOf(user.getUserId()), new String[]{}, new String[]{}, 60 * 60 * 24 * 30);
        user.setPassword(null);
        return JsonResult.ok("登录成功").put("access_token", token.getAccessToken());
//                .put("user", user)
//                .put("session_key", session_key).put("open_id", open_id);

    }

    /**
     * 修改个人信息
     */
    @PostMapping("/auth")
    @ResponseBody
    public JsonResult updateMyInfo(User user, HttpServletRequest request) {
        return JsonResult.ok("已登陆");
    }

}